package com.ruibang.glass.portal.util;

import org.apache.commons.lang.StringUtils;

import java.util.concurrent.ThreadLocalRandom;


public class RandomUtil {

    public static String loginName(String prefix) {
        return StringUtils.isNotBlank(prefix) ? prefix.concat(suffix()) : "user".concat(suffix());
    }

    private static String suffix() {
        Long t = System.currentTimeMillis();
        String s = String.valueOf(t);
        String suffix = s.substring(s.length() - 6, s.length());
        return suffix;
    }


    public static String randomNumbers(int length) {
        return randomString("0123456789", length);
    }

    public static String randomString(String baseString, int length) {
        StringBuilder sb = new StringBuilder();
        if (length < 1) {
            length = 1;
        }

        int baseLength = baseString.length();

        for (int i = 0; i < length; ++i) {
            int number = getRandom().nextInt(baseLength);
            sb.append(baseString.charAt(number));
        }

        return sb.toString();
    }

    public static ThreadLocalRandom getRandom() {
        return ThreadLocalRandom.current();
    }

}
